package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.itheima.dao.MovementDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Movement;
import com.itheima.utils.GetRowsConditionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ${小帅帅}
 * @date 2019-11-04
 * 运动知识库的服务
 */
@Service(interfaceClass = MovementService.class)
@Transactional
public class MovementServiceImpl implements MovementService{

    @Autowired
    private MovementDao movementDao;


    //上传运动知识库
    @Override
    public void movementAdd(List<Movement> movementList) {

        if (movementList != null && movementList.size() > 0) {

            //遍历预约的数据，进行识别判断
            for (Movement moveList : movementList) {

                //持久层：根据id，查询是否已经进行了导入
                long count = movementDao.countMovement(moveList.getId());
                //判断
                if (count > 0) {

                    //查询持久层：当前数据库中存在，已经设置，则直接进行覆盖更新
                    movementDao.overrideMovement(moveList);
                } else {

                    //查询持久层：当前数据库中没有该日期，直接进行插入存储即可
                    movementDao.movementAdd(moveList);
                }

            }
        }
    }


    //运动知识库，分页查询展示
    @Override
    public PageResult findPage(QueryPageBean pageBean) throws Exception {
        //使用工具类，获取分页查询的条件
        String condition = GetRowsConditionUtils.getCondition(pageBean);

        //持久层：权限管理分页查询调用
        Page<Movement> page = movementDao.findPage(condition);

        //获取总记录数
        long total = page.getTotal();

        //获取当前页的显示结果
        List<Movement> rows = page.getResult();

        //传递参数，并返回
        return new PageResult(total, rows);
    }

}
